﻿@using BootstrapBlazor.Components

@code{
    protected override void OnInitialized()
    {
        base.OnInitialized();
        for (int i = 0; i < 100; i++)
        {
            Items2.Add(new Spec { id = i, Name = "Test01_name" + i });
        }

        SelectedRows2 = Items2.Take(5).Skip(3).ToList();
    }

    //public class Spec
    //{
    //    public int id { get; set; }
    //    public string Name { get; set; }
    //}
}



<Button Text="确定" Icon="fa fa-check-square-o" OnClick="@OnConfirm" />
<Button Text="清除" Icon="fa fa-check-square-o" OnClick="@OnClear" />
<p class="mt-3">选中：</p>
<ul class="ul-demo">
    @foreach (var item in SelectedRows2)
    {
        <li>@item.Name</li>
    }
</ul>


<Table @ref="t1"
       TItem="Spec"
       IsPagination="true"
       IsStriped="true" 
       IsBordered="true" 
       IsMultipleSelect="true"
       ShowToolbar="true" 
       @bind-SelectedRows="@SelectedRows2"
       ShowDefaultButtons="true" 
       AutoGenerateColumns="true"
       OnQueryAsync="@OnQueryAsync" 
       OnSaveAsync="@SaveAsync"
       OnDeleteAsync="@DeleteAsync"
       ShowSearch="true" 
       ShowExtendButtons="true"
       DoubleClickToEdit="true"
	   ShowColumnList=true /> 

@code{
    Table<Spec> t1{ get; set; }
    private List<Spec>? Items2 { get; set; } = new List<Spec>();

    public List<Spec> SelectedRows2 { get; set; } = new List<Spec>();
    //private IEnumerable<Spec> SelectedRows2 = new List<Spec>();
    private Task<QueryData<Spec>> OnQueryAsync(QueryPageOptions options)
    {
        // 设置记录总数
        var total = Items2.Count;

        // 内存分页
        var items = Items2.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<Spec>()
        {
            Items = items,
            TotalCount = total,
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true
        });
    }

    /// <summary>
    /// 选择结果回调方法
    /// </summary>
    [Parameter] public EventCallback<IEnumerable<Spec>> OnConfirms { get; set; }

    private async Task OnConfirm()
    {
        await OnConfirms.InvokeAsync(SelectedRows2);
    }
    private void OnClear()
    {
        SelectedRows2 = new List<Spec>();
   }
    /// <summary>
    /// 保存方法
    /// </summary>
    /// <param name="model"></param>
    /// <param name="changedType"></param>
    /// <returns></returns>
    public Task<bool> SaveAsync(Spec model, BootstrapBlazor.Components.ItemChangedType changedType)
    {
        // 增加数据演示代码

        return Task.FromResult(true);
    }


	protected Task<bool> DeleteAsync(IEnumerable<Spec> items)
	{
		items.ToList().ForEach(item =>
		Items2.Remove(item));
		return Task.FromResult(true);
	}
}
